/*
 * Copyright (c) 2011 Joe Kopena <tjkopena@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
*/

import flash.display.BitmapData;
import flash.geom.Rectangle;

import com.rocketshipgames.haxe.Context;
import com.rocketshipgames.haxe.Engine;
import com.rocketshipgames.haxe.AssetCache;

import com.rocketshipgames.haxe.Entity;
import com.rocketshipgames.haxe.gfx.primitives.Spritesheet;

class Ex10Spritesheet extends Engine
{

  public function new():Void
  {
    trace("Starting");
    super(640, 480);

    var yoshiSpr = new Spritesheet("assets/yoshi.png", 24, 32, true, true);
    yoshiSpr.setAnimation(0, [0, 1, 2, 3, 4]);
    yoshiSpr.setAnimation(1, [0, 1, 5, 5, 5, 3, 4]);

    var yoshi1:SpritesheetInstance = yoshiSpr.newInstance();
    var yoshi2:SpritesheetInstance = yoshiSpr.newInstance();
    yoshi2.flipped = true;
    var yoshi3:SpritesheetInstance = yoshiSpr.newInstance();
    yoshi3.mirrored = true;
    var yoshi4:SpritesheetInstance = yoshiSpr.newInstance();
    yoshi4.mirrored = yoshi4.flipped = true;

    var yoshi5:SpritesheetInstance = yoshiSpr.newInstance();
    yoshi5.play(0);

    var yoshi6:SpritesheetInstance = yoshiSpr.newInstance();
    yoshi6.play(0);
    yoshi6.mirrored = yoshi6.flipped = true;

    var yoshi7:SpritesheetInstance = yoshiSpr.newInstance();
    yoshi7.play(1);

    var yoshi8:SpritesheetInstance = yoshiSpr.newInstance();
    yoshi8.play(1);
    yoshi8.flipped = true;

    var yoshi9:SpritesheetInstance = yoshiSpr.newInstance();
    yoshi9.play(1);
    yoshi9.mirrored = true;

    var yoshi10:SpritesheetInstance = yoshiSpr.newInstance();
    yoshi10.play(1);
    yoshi10.mirrored = yoshi10.flipped = true;

    add(new Entity(yoshi1, 292, 204));
    add(new Entity(yoshi2, 324, 204));
    add(new Entity(yoshi3, 292, 244));
    add(new Entity(yoshi4, 324, 244));

    add(new Entity(yoshi5, 240, 168));
    add(new Entity(yoshi6, 376, 290));

    add(new Entity(yoshi7, 264, 204));
    add(new Entity(yoshi8, 352, 204));
    add(new Entity(yoshi9, 264, 244));
    add(new Entity(yoshi10, 352, 244));

    // end new
  }

  private override function render():Void
  {
    Context.buffer.fillRect(Context.bufferRectangle,
			    0xffffff);
    super.render();
    // end function render
  }

   // Entry point for haXe
   static public function main()
   {
     // com.rocketshipgames.haxe.Debug.enableConsoleTrace();

     var cache:AssetCache = AssetCache.instance();
     cache.addBitmapData("assets/yoshi.png");
     cache.lock(function(){ new Ex10Spritesheet(); });

     // end main
   }

  // end Ex10Spritesheet
}
